package com.dd.cloud.common.config.mybatis;

import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.TimeZone;

/**
 * shedlock分布式定时器配置类
 * 基于jdbc的锁, 锁默认30s
 */
@Configuration
//@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
@EnableSchedulerLock(mode = EnableSchedulerLock.InterceptMode.PROXY_METHOD, defaultLockAtMostFor = "PT1S")
public class ShedLockConfig {

    @Resource
    private DataSource dataSource;

    /**
     * @description
     * @date 2021/1/10 22:39
     */
    @Bean
    public LockProvider lockProvider() {
        return new JdbcTemplateLockProvider(
                JdbcTemplateLockProvider.Configuration.builder()
                        .withJdbcTemplate(new JdbcTemplate(dataSource))
                        .withTimeZone(TimeZone.getTimeZone("GMT+8"))
                        .build()
        );
    }
}
